package com.example.zhinongbao.view;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.zhinongbao.R;
import com.example.zhinongbao.contract.SaleHomeContact;
import com.example.zhinongbao.entity.TitleInfo;
import com.example.zhinongbao.presenter.SaleHomePresenter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;



public class FragmentHome2 extends Fragment implements View.OnClickListener, SaleHomeContact {
    //    private SaleHomePresenter saleHomePresenter;
    private ImageButton GoBuy;
    private View sale_home;

    private LinearLayout wodehuopin, caigouguanli, wodedingdan,xinwenzixun;
    private List<TitleInfo> titles=new ArrayList<>();
    private TabLayout tab_layout;
    private ViewPager2 viewPager;
    public FragmentHome2() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (sale_home == null) {
            sale_home = inflater.inflate(R.layout.fragment_home2, container, false);
        }
        titles.add(new TitleInfo("推荐","top"));
        titles.add(new TitleInfo("国内","guonei"));
        titles.add(new TitleInfo("国际","guoji"));
        titles.add(new TitleInfo("娱乐","yule"));
        titles.add(new TitleInfo("体育","tiyu"));
        titles.add(new TitleInfo("军事","junshi"));
        titles.add(new TitleInfo("科技","keji"));
        titles.add(new TitleInfo("财经","cajing"));
        titles.add(new TitleInfo("游戏","youxi"));
        titles.add(new TitleInfo("汽车","qiche"));
        titles.add(new TitleInfo("健康","jiankang"));
        //初始化控件
        tab_layout = sale_home.findViewById(R.id.tab_layout);
        viewPager = sale_home.findViewById(R.id.viewPager);
        wodedingdan= sale_home.findViewById(R.id.OrderManagement);
//       saleHomePresenter = new SaleHomePresenter(this);
        GoBuy = sale_home.findViewById(R.id.go_buy);
        onGoBuyButtonClick();
        wodehuopin = sale_home.findViewById(R.id.MyProducts);
        wodehuopin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), ProductActivity.class);
                startActivity(intent);
            }
        });
        caigouguanli = sale_home.findViewById(R.id.shopguanli);
        caigouguanli.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), CaigouActivity.class);
                startActivity(intent);
            }
        });

        xinwenzixun = sale_home.findViewById(R.id.OrderManagement);
        xinwenzixun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

wodedingdan.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent =new Intent(getActivity(),OrdersActivity.class);
        intent.putExtra("case",1);
        startActivity(intent);
    }
});



        viewPager.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                //创建 NewsTabFragment页面
                String title = titles.get(position).getPy_title();
                TabNewsFragment tabNewsFragment = TabNewsFragment.newInstance(title);
                return tabNewsFragment;
            }

            @Override
            public int getItemCount() {
                return titles.size();
            }
        });
        tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //设置viewPager选中当前页
                viewPager.setCurrentItem(tab.getPosition(), false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }


            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //将tab_layout和 viewPager关联在一起
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tab_layout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles.get(position).getTitle());
            }
        });
        //这几话不能少
        tabLayoutMediator.attach();
        return sale_home;
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.go_buy) {
            Intent intent = new Intent(getActivity(), MainActivity.class);
            startActivity(intent);
            getActivity().finish();

        }
    }

    @Override
    public void onGoBuyButtonClick() {

        GoBuy.setOnClickListener(this);
    }
}